package com.eorchis.module.trainneedscope.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.core.ui.commond.ICommond;
import com.eorchis.module.TrainingConstants;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.service.cache.BaseDataCacheUtil;
import com.eorchis.module.trainingquestionnaire.domain.QuestionnaireEntity;
import com.eorchis.module.trainingquestionnaire.service.IQuestionnaireService;
import com.eorchis.module.trainingquestionnaire.ui.commond.QuestionnaireQueryCommond;
import com.eorchis.module.trainingquestionnaire.ui.commond.QuestionnaireValidCommond;
import com.eorchis.module.trainneedscope.dao.ITrainNeedScopeDao;
import com.eorchis.module.trainneedscope.domain.QueryClassUserBean;
import com.eorchis.module.trainneedscope.domain.TrainNeedScope;
import com.eorchis.module.trainneedscope.domain.TrainNeedScopeQueryBean;
import com.eorchis.module.trainneedscope.event.AddDeptOrUserEvent;
import com.eorchis.module.trainneedscope.service.ITrainNeedScopeService;
import com.eorchis.module.trainneedscope.ui.commond.TrainNeedScopeQueryCommond;
import com.eorchis.module.trainneedscope.ui.commond.TrainNeedScopeValidCommond;

/**
 * Title: TrainNeedScopeServiceImpl<br>
 * Description: 培训需求调查对象管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-08-27
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.trainneedscope.service.impl.TrainNeedScopeServiceImpl")
public class TrainNeedScopeServiceImpl 
			extends AbstractBaseService 
		implements ITrainNeedScopeService,ApplicationContextAware,ApplicationListener<AddDeptOrUserEvent> {
		
	@Autowired
	@Qualifier("com.eorchis.module.trainneedscope.dao.impl.TrainNeedScopeDaoImpl")
	private ITrainNeedScopeDao trainNeedScopeDao;
	
	@Resource(name = "com.eorchis.module.basedata.service.cache.BaseDataCacheUtil")
	private BaseDataCacheUtil baseDataCacheUtil;
	
	@Autowired
	@Qualifier("main.java.com.eorchis.module.trainingquestionnaire.service.impl.QuestionnaireServiceImpl")
	private IQuestionnaireService questionnaireService;
	
	private ApplicationContext applicationContext;
		

	public IDaoSupport getDaoSupport() {
		return trainNeedScopeDao;
	}
	@SuppressWarnings("unchecked")
	public TrainNeedScopeValidCommond toCommond(IBaseEntity entity) {
		return new TrainNeedScopeValidCommond((TrainNeedScope)entity);
	}

	/**
	 * 添加人员或部门到问卷范围
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
	public boolean addEntityIntoTrainNeedScope(TrainNeedScopeValidCommond vCommond)
			throws Exception {
		 
		trainNeedScopeDao.addEntityIntoTrainNeedScope(vCommond);
		this.applicationContext.publishEvent(new AddDeptOrUserEvent(vCommond));
		return true;
	}
	
	/**
	 * 查询问卷下的人员列表
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
	public List<TrainNeedScopeQueryBean> getUserListByTrainNeedScope(TrainNeedScopeQueryCommond qCommond) throws Exception{
		return trainNeedScopeDao.getUserListByTrainNeedScope(qCommond);
	}
	
	/**
	 * 查询问卷下部门列表
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
	public List<TrainNeedScopeQueryBean> getDeptListByTrainNeedScope(TrainNeedScopeQueryCommond qCommond) throws Exception{
		
		
		//将职级code转换成名称
		List<TrainNeedScopeQueryBean> list=trainNeedScopeDao.getDeptListByTrainNeedScope(qCommond);
		
		//获取数据字典职级的数据字典集合
		Map<String,List<BaseData>> baseDataMap =  baseDataCacheUtil.getBaseDataList();
		List<BaseData>  baseDataList=new ArrayList<BaseData>();
		baseDataList=baseDataMap.get(TrainingConstants.LEVEL_CODE);
		Map<String,String> baseDate=new HashMap<String,String>();
		for(int i=0;i<baseDataList.size();i++) {
			BaseData b=baseDataList.get(i);
			baseDate.put(b.getDataCode(), b.getDataName());
		}
		
		if(list!=null&&list.size()>0) {
			for(int i=0;i<list.size();i++) {
				String dutyNames="";
				TrainNeedScopeQueryBean bean=list.get(i);
				String codes=bean.getDutyCodes();
				if(codes!=null&&!"".equals(codes)) {
					String [] code=codes.split(",");
					
					//进行查人数的逻辑
					Integer num=trainNeedScopeDao.getUserNumByScopeId(bean.getLinkId(),code);
					bean.setUserCount(num);
					for(int j=0;j<code.length;j++) {
						if(baseDate.get(code[j])!=null) {
							if(!"".endsWith(dutyNames)) {
								dutyNames=dutyNames+","+baseDate.get(code[j]);
							}else {
								dutyNames=dutyNames+baseDate.get(code[j]);
							}
						}
					}
				}else {
					Integer num=trainNeedScopeDao.getUserNumByScopeId(bean.getLinkId(),null);
					bean.setUserCount(num);
				}
				bean.setDutyNames(dutyNames);
			}
		}
		
		return list;
	}
	
	/**
	 * 查询未选人员列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
	public List<QueryClassUserBean> findNoSelectedUserList(TrainNeedScopeQueryCommond qCommond)
			throws Exception {
		return trainNeedScopeDao.findNoSelectedUserList(qCommond);
	}
	
	/**
	 * 查询未选部门列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
	public List<QueryClassUserBean> findNoSelectedDeptList(TrainNeedScopeQueryCommond qCommond)
			throws Exception {
		return trainNeedScopeDao.findNoSelectedDeptList(qCommond);
	}
	
	/**
	 * 删除问卷范围关联关系表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
	public void deleteTargetScopeLink(TrainNeedScopeValidCommond vCommond) throws Exception {
		trainNeedScopeDao.deleteTargetScopeLink(vCommond);
	}
	
	/**
	 * 更改问卷范围内的职级范围
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-27
	 */
	@Override
   public void updateTargerScopeDuty(TrainNeedScopeValidCommond vCommond) throws Exception{
		trainNeedScopeDao.updateTargerScopeDuty(vCommond);
		this.applicationContext.publishEvent(new AddDeptOrUserEvent(vCommond));
	}
	
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	/*
	 * 事件
	 * 
	 */
	@Override
	public void onApplicationEvent(AddDeptOrUserEvent event) {
		TrainNeedScopeValidCommond vCommond=(TrainNeedScopeValidCommond) event.getSource();
		QuestionnaireValidCommond v=questionnaireService.find(vCommond.getSearchQuestionNaireId());
		//TrainNeedScopeValidCommond v=this.find(vCommond.getSearchQuestionnaireId());
		QuestionnaireQueryCommond queryCommond=new QuestionnaireQueryCommond();
		try {
			
			if(v.getPublishState()!=null&&v.getPublishState().equals(QuestionnaireEntity.IS_PUBLISH_Y)) {
			       Date time=new Date();
				 if(time.before(v.getEndDate()) && time.after(v.getBeginDate())) {
					 queryCommond.setSearchQuestionnaireIds(new String [] {v.getQuestionnaireId()});
					String msg=questionnaireService.queryQuestionnaireSurveyScope(queryCommond);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
